﻿using AbpDemo.Authors.Dto;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;
using System.Threading;
using CSRedis;
using Microsoft.AspNetCore.Authorization;
using AbpDemo.Articles.Dto;
using Newtonsoft.Json;

namespace AbpDemo.Articles
{
    [ApiExplorerSettings(GroupName = "v2")]
    [Authorize]
    public class ArticleService : CrudAppExtService<Article, ArticleDto, Guid, CreateUpdateArticleDto>, IArticlesService
    {
        private readonly CSRedisClient client;

        public ArticleService(IRepository<Article, Guid> repository, CSRedisClient client) : base(repository)
        {
            this.client = client;
        }

        public override async Task<ResultDto<ArticleDto>> CreateAsync(CreateUpdateArticleDto createInput)
        {
            //向数据库插入数据
            await base.CreateAsync(createInput);

            /*//发送邮件
            Thread.Sleep(TimeSpan.FromSeconds(60));*/

            //向队列推送数据
            for (int i = 0; i < 30; i++)
            {
                EmailDto emailDto = new EmailDto
                {
                    Email = $"{Guid.NewGuid()}@126.com",
                    EmailSubject = createInput.Title,
                    EmailContent = createInput.Content
                };

                await client.PublishAsync("channel.1",JsonConvert.SerializeObject(emailDto));

                //await client.LPushAsync($"{nameof(Article)}:{nameof(EmailDto)}", emailDto);
            }
            return new ResultDto<ArticleDto> { Code = ResultState.Success, Message = "发布成功" };
        }
    }
}
